<?php
/**
 * tpshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: 当燃 2016-01-09
 */
namespace app\mobile\controller;
use app\home\logic\UsersLogic;
use Think\Db;
class Index extends MobileBase {

    public function index(){
         
        /*
         //获取微信配置
         $wechat_list = M('wx_user')->select();
         $wechat_config = $wechat_list[0];
         $this->weixin_config = $wechat_config;
         // 微信Jssdk 操作类 用分享朋友圈 JS
         $jssdk = new \Mobile\Logic\Jssdk($this->weixin_config['appid'], $this->weixin_config['appsecret']);
         $signPackage = $jssdk->GetSignPackage();
         print_r($signPackage);
         */
        
        $hot_goods = M('goods')->where("is_hot=1 and is_on_sale=1")->order('goods_id DESC')->limit(20)->cache(true,TPSHOP_CACHE_TIME)->select();//首页热卖商品
        $thems = M('goods_category')->where('level=1')->order('sort_order')->limit(9)->cache(true,TPSHOP_CACHE_TIME)->select();
        $this->assign('thems',$thems);
        $this->assign('hot_goods',$hot_goods);
        $favourite_goods = M('goods')->where("is_recommend=1 and is_on_sale=1")->order('goods_id DESC')->limit(20)->cache(true,TPSHOP_CACHE_TIME)->select();//首页推荐商品
        
        //秒杀商品
        $now_time = time();  //当前时间
        if(is_int($now_time/7200)){      //双整点时间，如：10:00, 12:00
            $start_time = $now_time;
        }else{
            $start_time = floor($now_time/7200)*7200; //取得前一个双整点时间
        }
        $end_time = $start_time+7200;   //结束时间
        $seckill_list=DB::query("select * from __PREFIX__goods as g inner join __PREFIX__flash_sale as f on g.goods_id = f.goods_id where start_time = $start_time and end_time = $end_time limit 3");     //获取秒杀商品
        //热销商品
        $hotGoods_list = M('goods')->where("is_on_sale=1 and type=0 and prom_type=0")->order("sales_sum desc")->limit(4)->select();
        //一元夺宝
        $indiana_sale_list = M('indiana_sale')
                            ->alias("s")
                            ->join("f_goods g","s.goods_id=g.goods_id","left")
                            ->where("$now_time >s.start_time and $now_time< s.end_time and s.is_end=0")
                            ->order("id desc")
                            ->field("s.*,g.original_img,g.market_price")
                            ->limit(3)
                            ->select();
        foreach ($indiana_sale_list as $key=>$value){
            $indiana_sale_list[$key]['end_time'] = date("Y/m/d H:i:s",$value['end_time']);
        }
        

        $this->assign("indiana_sale_list",$indiana_sale_list);
        $this->assign("hotGoods_list",$hotGoods_list);
        $this->assign('seckill_list',$seckill_list);
        $this->assign('start_time',$start_time);
        $this->assign('end_time',$end_time);
        $this->assign('favourite_goods',$favourite_goods);
        return $this->fetch();
    }

    /**
     * 分类列表显示
     */
    public function categoryList(){
        return $this->fetch();
    }

    /**
     * 模板列表
     */
    public function mobanlist(){
        $arr = glob("D:/wamp/www/svn_tpshop/mobile--html/*.html");
        foreach($arr as $key => $val)
        {
            $html = end(explode('/', $val));
            echo "<a href='http://www.php.com/svn_tpshop/mobile--html/{$html}' target='_blank'>{$html}</a> <br/>";            
        }        
    }
    
    /**
     * 商品列表页
     */
    public function goodsList(){
        $id = I('get.id',0); // 当前分类id
        $lists = getCatGrandson($id);
        $this->assign('lists',$lists);
        return $this->fetch();
    }
    
    public function ajaxGetMore(){
    	$p = I('p/d',1);
    	$favourite_goods = M('goods')->where("is_recommend=1 and is_on_sale=1")->order('goods_id DESC')->page($p,10)->cache(true,TPSHOP_CACHE_TIME)->select();//首页推荐商品
    	$this->assign('favourite_goods',$favourite_goods);
    	return $this->fetch();
    }

    // 促销活动页面
    public function promoteList()
    {
        $goodsList = DB::query("select * from __PREFIX__goods as g inner join __PREFIX__flash_sale as f on g.goods_id = f.goods_id   where ".time()." > start_time  and ".time()." < end_time");
        $brandList = M('brand')->limit(5)->getField("id,name,logo");
        $this->assign('brandList',$brandList);
        $this->assign('goodsList',$goodsList);
        return $this->fetch();
    }

    // 加载更多促销活动商品
    public function ajaxPromoteList(){
        $p = ((I('p/d')*5))+1;
        $goodsList = DB::query("select * from __PREFIX__goods as g inner join __PREFIX__flash_sale as f on g.goods_id = f.goods_id   where ".time()." > start_time  and ".time()." < end_time");
        $brandList = M('brand')->page($p,5)->getField("id,name,logo");
        $this->assign('brandList',$brandList);
        $this->assign('goodsList',$goodsList);
        return $this->fetch();
    }
    
    //专题活动
    public function special(){
    	$id = I('get.id/d');
    	$id = empty($id)?'1':$id;
    	$this->assign('id',$id);
    	return $this->fetch();
    }
    
    //抽奖活动(转盘)
    public function dial(){
       echo "<center>本活动暂时关闭</center>";
       exit();
       $user_id = $_SESSION['user']['user_id'];
       if(!empty($user_id)){
           $user_info = M("users")->where("user_id",$user_id)->find();
       }else{
           $user_info['pay_points'] = 0;
           $user_id =0;
       }
       if(IS_AJAX){//转盘转动
           if (empty($user_id)) {
               $back['status'] = -1;
               $back['msg'] = "未登陆,请先登陆";
               $back['data'] = "未登陆,请先登陆";
               echo  json_encode($back);
               exit();
           }
           if(empty($user_info['mobile'])){
               $back['status'] = 0;
               $back['msg'] = "抽奖失败,未绑定手机";
               $back['data'] = "抽奖失败,未绑定手机";
               echo  json_encode($back);
               exit();
           }
           if($user_info['is_lock']){
               $back['status'] = 0;
               $back['msg'] = "抽奖失败,账户被冻结";
               $back['data'] = "账户被冻结";
               echo json_encode($back);
               exit();
           }
           if($user_info['pay_points'] <500){
               $back['status'] = 0;
               $back['msg'] = "抽奖失败,银酒币不足500";
               $back['data'] = "抽奖失败,银酒币不足500";
               echo  json_encode($back);
               exit();
           }
           //扣除用户500银酒币
           $data['pay_points'] = $user_info['pay_points']-500;
           M('users')->where("user_id",$user_id)->save($data);
           income_action(0,3,"银酒币转盘抽奖",-500,$user_id);//记录日志
           $prize_arr = C("prize_arr");//奖品配置
           foreach ($prize_arr as $key => $val) {
               $arr[$val['id']] = $val['v'];
           }
           $rid = getRand($arr); //根据概率获取奖项id
           $result['level'] = $rid;
           switch ($rid){
               case 1:
                   break;
               case 2:
                   break;
               case 3:
                   $price = rand(5,18)/10; //红包金额
                   $where['user_money'] = $user_info['user_money'] + $price;
                   M('users')->where("user_id",$user_id)->save($where);
                   income_action(2,3,"银酒币转盘抽奖",$price,$user_id);//记录日志
                   $result['num'] = $price;
                   $result['pay_points'] = $data['pay_points'];
                   break;
               case 4:
                   $data['pay_points'] = $user_info['pay_points'];
                   M('users')->where("user_id",$user_id)->save($data);
                   income_action(0,3,"银酒币转盘抽奖",500,$user_id);//记录日志
                   $result['num'] = 500;
                   $result['pay_points'] = $data['pay_points'];
                   break;
               case 5:
                   $data['pay_points'] = $user_info['pay_points']-400;
                   M('users')->where("user_id",$user_id)->save($data);
                   income_action(0,3,"银酒币转盘抽奖",100,$user_id);//记录日志
                   $result['num'] = 100;
                   $result['pay_points'] = $data['pay_points'];
                   break;
           }
           $result['pay_points'] = sprintf("%.2f", $result['pay_points']); 
           $back['status'] = 1;
           $back['msg'] = "抽奖成功";
           $back['data'] = $result;
           echo  json_encode($back);
           exit();
           //计算huojiang
           
       }
       $this->assign("user_id",$user_id);
       $this->assign("pay_points",$user_info['pay_points']);
       return $this->fetch();
    }
    
    
    
    //抽奖活动(转盘)
    public function dial_record(){
        echo "<center>本活动暂时关闭</center>";
        exit();
        //查询最近50条抽奖记录
        $dial_record =  M('user_income')
                            ->alias("ui")
                            ->join("f_users u","ui.uid = u.user_id")
                            ->where("source_name='银酒币转盘抽奖' and amount >0")
                            ->order("id desc")
                            ->field("ui.*,u.mobile")
                            ->limit(50)
                            ->select();

        $first_gift = rand(5,50);
        $second_gift = rand(5,50);
        $first_data = array("mobile"=>get_rand_mobile(),"desc"=>"长征纪念酒");
        $second_data = array("mobile"=>get_rand_mobile(),"desc"=>"上品传奇酒");
        foreach ($dial_record as $key=>$value){
            if($value['amount'] == 100){
                $dial_record[$key]['desc'] = "奖励100银酒币";
            }elseif($value['amount'] == 500){
                $dial_record[$key]['desc'] = "奖励500银酒币";
            }else{
                $dial_record[$key]['desc'] = "奖励".$value['amount']."元红包";
            }
            if($key == $first_gift){
                $first_data['add_time'] = $value['add_time'];
                $dial_record[$key] = $first_data;
            }
            if($key == $second_gift){
                $second_data['add_time'] = $value['add_time'];
                $dial_record[$key] = $second_data;
            }
            $dial_record[$key]['mobile'] = substr_replace($dial_record[$key]['mobile'],'****',3,4);
        }
        //查询我的记录
        $user_id = $_SESSION['user']['user_id'];
        if(!empty($user_id)){
            $my_dial_record =  M('user_income')
            ->alias("ui")
            ->join("f_users u","ui.uid = u.user_id")
            ->where("source_name='银酒币转盘抽奖' and amount >0 and uid=$user_id")
            ->order("id desc")
            ->field("ui.*,u.mobile")
            ->select();
            foreach ($my_dial_record as $key=>$value){
                if($value['amount'] == 100){
                    $my_dial_record[$key]['desc'] = "奖励100银酒币";
                }elseif($value['amount'] == 500){
                    $my_dial_record[$key]['desc'] = "奖励500银酒币";
                }else{
                    $my_dial_record[$key]['desc'] = "奖励".$value['amount']."元红包";
                }
                $my_dial_record[$key]['mobile'] = substr_replace($my_dial_record[$key]['mobile'],'****',3,4);
            }
        }
        $this->assign("my_dial_record",$my_dial_record);
        $this->assign("dial_record",$dial_record);
        return $this->fetch();
    }
    
    
    
    public function mytest(){
    	
    }
    
 
}